package validator

import (
	"context"
	"encoding/json"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"jk-time/webook/pkg/logger"
	"jk-time/webook/pkg/migrator/events"
	"time"
)

type baseValidator struct {
	base   *gorm.DB
	target *gorm.DB

	// 以src或dst为准，修复数据需要找到
	direction string
	l         logger.Logger
	p         events.Producer
}

func NewBaseValidator(base *gorm.DB, target *gorm.DB, direction string, l logger.Logger, p events.Producer) baseValidator {
	return baseValidator{base: base, target: target, direction: direction, l: l, p: p}
}
func (v *baseValidator) notify(ctx context.Context, id int64, typ string) {
	dbCtx, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	evt := events.InconsistentEvent{
		ID:        id,
		Direction: v.direction,
		Type:      typ,
	}
	err := v.p.ProduceInconsistentEvent(dbCtx, evt)
	if err != nil {
		evts, _ := json.Marshal(&evt)
		zap.L().Error("发送消息失败", zap.Error(err), zap.String("event", string(evts)))
	}
}
